package gek.uni.aspirant.ocl.rcp.command.handlers;

import gek.uni.aspirant.ocl.types.prolog.PrologGeneratorResult;
import gek.uni.aspirant.uml.utils.OclUtils;
import gek.uni.aspirant.uml.utils.wizzards.SelectUmlElementWizard;

import java.util.List;
import java.util.Map.Entry;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ocl.expressions.OCLExpression;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;

public class SelectModelElementHandler extends AbstractHandler implements
    IHandler {

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell parentShell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
    SelectUmlElementWizard selectUmlElementWizard = new SelectUmlElementWizard();
    WizardDialog dialog = new WizardDialog(parentShell, selectUmlElementWizard);
    dialog.setMinimumPageSize(150, 300);

    if (dialog.open() != Window.CANCEL) {
      Element umlContext = selectUmlElementWizard.getUmlContext();
      List<String> expressions = selectUmlElementWizard.getExpressions();
      try {
        PrologGeneratorResult generatorResult = OclUtils
            .processUmlOclExpression(umlContext, expressions);             
        System.out.println(generatorResult.getCodeGeneratorPair().getCode());
        System.out.println("Essential MAP:");
        for (Entry<String, OCLExpression> entry : generatorResult
            .getEssentialMap().entrySet()) {
          System.out.println(entry.getKey().toString() + " ---> "
              + entry.getValue().toString());
        }        
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return null;
  }

}
